/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.web.servlet.admin.menu;

import com.supinfo.youfood.entity.Employe;
import com.supinfo.youfood.entity.Menu;
import com.supinfo.youfood.entity.Restaurant;
import com.supinfo.youfood.service.MenuService;
import com.supinfo.youfood.service.RestaurantService;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author TaKeO
 */
@WebServlet(name = "DeleteMenuServlet", urlPatterns = {"/admin/delete-menu"})
public class DeleteMenuServlet extends HttpServlet {
    
    @EJB
    private MenuService menuService;
    
    @EJB
    private RestaurantService restoService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        if(request.getParameter("id") != null)
        {
            Menu menu = menuService.findMenuById(new Long(request.getParameter("id")));
            Employe employe = (Employe) request.getSession().getAttribute("currentEmp");
            Restaurant resto = restoService.findRestoById(employe.getResto().getId_restaurant());
            
            if(menu != null)
            {
                if(menu.getDansRestaurants().contains(resto))
                {
                    resto.removeMenu(menu);
                    menu.removeRestaurant(resto);
                }
                else
                {
                    resto.addMenu(menu);
                    menu.addRestaurant(resto);
                }
                restoService.refreshResto(resto);
                menuService.refreshMenu(menu);              
            }
        }
        
        response.sendRedirect("/YouFood/admin/liste-menus");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        
    }
}
